 /*:
 * @plugindesc 楓之舞功能 - 角色腳步聲
 * @author 老饕
 * @version 1.0.0
 * @date 2020.3.15
 *
 * @param ---腳步聲---
 * @default
 *
 * @param Enable Sound
 * @parent ---腳步聲---
 * @type boolean
 * @on Enable
 * @off Disable
 * @desc 默認是否啟用腳步聲？
 * 否 - false     是 - true
 * @default true
 *
 * @param Sound Name
 * @parent ---腳步聲---
 * @type file
 * @dir audio/se/
 * @require 1
 * @desc SE 文件名（區分大小寫）
 * 不包括擴展名。
 * @default Cursor1
 *
 * @param Sound Volume
 * @parent ---腳步聲---
 * @type number
 * @min 0
 * @desc 腳步聲音量。
 * @default 50
 *
 * @param Volume Variance
 * @parent ---腳步聲---
 * @desc 腳步聲音量變化方差。
 * @default 10
 *
 * @param Sound Pitch
 * @parent ---腳步聲---
 * @desc 腳步聲音調。
 * @default 100
 *
 * @param Pitch Variance
 * @parent ---腳步聲---
 * @desc 腳步聲音調變化方差。
 * @default 10
 *
 * @help 玩家行走時自動播放腳步聲音效，並自動根據人物位置調整聲像。
 * 防止腳步聲混亂，目前僅支持第一個玩家走步聲。
 */


var SWDA_StepSound = SWDA_StepSound || {};
var params = PluginManager.parameters('SWDA_StepSound');

SWDA_StepSound.StepSoundEnable = eval(String(params['Enable Sound']));
SWDA_StepSound.StepSoundName = String(params['Sound Name']);
SWDA_StepSound.StepSoundVolume = Number(params['Sound Volume']);
SWDA_StepSound.StepVolumeVar = Number(params['Volume Variance']);
SWDA_StepSound.StepSoundPitch = Number(params['Sound Pitch']);
SWDA_StepSound.StepPitchVar = Number(params['Pitch Variance']);

//=============================================================================
// Game_CharacterBase
//=============================================================================
SWDA_StepSound.Game_Player_updatePattern = Game_Player.prototype.updatePattern;
Game_Player.prototype.updatePattern = function() {
	SWDA_StepSound.Game_Player_updatePattern.call(this);
	var stepSoundPan = (this.screenX()-Graphics.boxWidth/2)/Graphics.boxWidth*200;
	//var soundStep = ((this._pattern + 1) % (2 * (2 - this.isDashing())) == 0);
	var soundStep = ((this._pattern + 1) % (Math.floor(this._maxFrame/2)) == 0);
	if (this.isMoving() && soundStep){
		SoundManager.playStepSound(stepSoundPan,true);
	}
};

/*跟隨玩家音效，備用
SWDA_StepSound.Game_Follower_updatePattern = Game_Follower.prototype.updatePattern;
Game_Follower.prototype.updatePattern = function() {
	SWDA_StepSound.Game_Follower_updatePattern.call(this);
	var stepSoundPan = (this.screenX()-Graphics.boxWidth/2)/Graphics.boxWidth*200;
	//var soundStep = ((this._pattern + 1) % (2 * (2 - this.isDashing())) == 0);
	var soundStep = ((this._pattern + 1) % (Math.floor(this._maxFrame/2)) == 0);
	if (this.isMoving() && soundStep){
		SoundManager.playStepSound(stepSoundPan);
	}
};
*/

//=============================================================================
// SoundManager
//=============================================================================

SoundManager.playStepSound = function(stepSoundPan, isPlayer) {
	var isPlayer = isPlayer || false;
	if (isPlayer)
		AudioManager.playSe($gameSystem.getStepSound(stepSoundPan,true));
	else//跟隨玩家音效不同
		AudioManager.playSe($gameSystem.getStepSound(stepSoundPan,false));
};

//=============================================================================
// Game_System
//=============================================================================


Game_System.prototype.initStepSounds = function() {
    this._stepSoundEnable = SWDA_StepSound.StepSoundEnable;
    this._stepSoundName = SWDA_StepSound.StepSoundName;
    this._stepSoundVol = SWDA_StepSound.StepSoundVolume;
	this._stepSoundVolVar = SWDA_StepSound.StepVolumeVar;
    this._stepSoundPitch = SWDA_StepSound.StepSoundPitch;
    this._stepSoundPitchVar = SWDA_StepSound.StepPitchVar;
};



Game_System.prototype.getStepSound = function(stepSoundPan, isPlayer) {
	var isPlayer = isPlayer || false;
    if (this._stepSoundName === undefined) this.initStepSounds();
    var obj = {
      name: this._stepSoundName,
      volume: this._stepSoundVol,
      pitch: this._stepSoundPitch,
      pan: stepSoundPan
    }
	
	var max = this._stepSoundVol + this._stepSoundVolVar;
    var min = this._stepSoundVol - this._stepSoundVolVar;
	if (isPlayer){
		obj['volume'] = Math.floor(Math.random() * ( max - min + 1) + min);
	}else{
		obj['volume'] = Math.floor(Math.random() * ( max - min + 1) + min)*0.2;
	}
	
    var max = this._stepSoundPitch + this._stepSoundPitchVar;
    var min = this._stepSoundPitch - this._stepSoundPitchVar;
    obj['pitch'] = Math.floor(Math.random() * ( max - min + 1) + min);
	
	/*
    var max = this._stepSoundPan + this._stepSoundPanVar;
    var min = this._stepSoundPan - this._stepSoundPanVar;
    obj['pan'] = Math.floor(Math.random() * ( max - min + 1) + min);
	*/
	//obj['pan'] = 0;
    return obj;
};